package com.cls.business.controller;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.utils.MyUtil;
import com.cls.common.entity.MyConstant;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.github.pagehelper.PageInfo;
import com.cls.business.entity.ExpertGroup;
import com.cls.business.service.IExpertGroupService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 *  Controller
 *
 * @author wandering
 * @date 2020-11-30 16:35:23
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class ExpertGroupController extends BaseController {

    private final IExpertGroupService expertGroupService;

    /**
    * 列表页面
    **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/expertGroup")
    public String expertGroupIndex(){
        return MyUtil.view("business/expertGroup/expertGroup");
    }

    /**
    * 新增页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/expertGroup/expertGroupAdd")
    public String expertGroupAdd(Model model){
        return MyUtil.view("business/expertGroup/expertGroupAdd");
    }

    /**
    * 修改页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/expertGroup/expertGroupUpdate/{expertGroupId}")
    public String expertGroupUpdate(@PathVariable Long expertGroupId, Model model){
        ExpertGroup expertGroup = expertGroupService.getById(expertGroupId);
        model.addAttribute("expertGroup",expertGroup);
        return MyUtil.view("business/expertGroup/expertGroupUpdate");
    }



    /**
    * 详情页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/expertGroup/expertGroupDetail/{expertGroupId}")
    public String expertGroupDetail(@PathVariable Long expertGroupId, Model model){
        ExpertGroup expertGroup = expertGroupService.getById(expertGroupId);
        model.addAttribute("expertGroup",expertGroup);
        return MyUtil.view("business/expertGroup/expertGroupDetail");
    }

    @GetMapping("expertGroup")
    @ResponseBody
    @RequiresPermissions("expertGroup:list")
    public ResponseVO getAllExpertGroups(ExpertGroup expertGroup) {
        return ResponseVO.success(expertGroupService.findExpertGroups(expertGroup));
    }

    @GetMapping("expertGroup/list")
    @ResponseBody
    @RequiresPermissions("expertGroup:list")
    public ResponseVO expertGroupList(QueryRequest request, ExpertGroup expertGroup) {
        PageInfo<ExpertGroup> pageInfo =  this.expertGroupService.findExpertGroups(request, expertGroup);
        return ResponseVO.success(pageInfo);
    }

    @ControllerEndpoint(operation = "新增ExpertGroup", exceptionMessage = "新增ExpertGroup失败")
    @PostMapping("expertGroup")
    @ResponseBody
    @RequiresPermissions("expertGroup:add")
    public ResponseVO addExpertGroup(@Valid ExpertGroup expertGroup) {
        this.expertGroupService.createExpertGroup(expertGroup);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除ExpertGroup", exceptionMessage = "删除ExpertGroup失败")
    @PostMapping("expertGroup/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("expertGroup:delete")
    public ResponseVO deleteExpertGroups(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.expertGroupService.deleteExpertGroups(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个ExpertGroup", exceptionMessage = "删除ExpertGroup失败")
    @PostMapping("expertGroup/delete/{expertGroupId}")
    @ResponseBody
    @RequiresPermissions("expertGroup:delete")
    public ResponseVO deleteExpertGroup(@PathVariable Long expertGroupId) {
        this.expertGroupService.removeById(expertGroupId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改ExpertGroup", exceptionMessage = "修改ExpertGroup失败")
    @PostMapping("expertGroup/update")
    @ResponseBody
    @RequiresPermissions("expertGroup:update")
    public ResponseVO updateExpertGroup(ExpertGroup expertGroup) {
        this.expertGroupService.updateExpertGroup(expertGroup);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改ExpertGroup", exceptionMessage = "导出Excel失败")
    @PostMapping("expertGroup/excel")
    @ResponseBody
    @RequiresPermissions("expertGroup:export")
    public void export(QueryRequest queryRequest, ExpertGroup expertGroup, HttpServletResponse response) {
        List<ExpertGroup> expertGroups = this.expertGroupService.findExpertGroups(queryRequest, expertGroup).getList();
        ExcelKit.$Export(ExpertGroup.class, response).downXlsx(expertGroups, false);
    }
}
